<?php 
 
namespace app\common\model;

use app\common\model\BaseModel;
use app\common\traits\ModelTrait;
use app\common\utils\JsonService;
use think\facade\Db;

/**
* This is the model class for table "{ptf_member_address}".
*
* @property int $address_id 表id 
* @property int $member_id 用户id 
* @property string $consignee 收货人
* @property string $email 邮箱地址
* @property int $country 国家 
* @property int $province 省份 
* @property int $city 城市 
* @property int $district 地区 
* @property int $twon 乡镇 
* @property string $address 地址
* @property string $zipcode 邮政编码
* @property string $mobile 手机
* @property int $is_default 默认收货地址 
* @property int $is_pickup  
*/
class MemberAddress extends BaseModel{

    protected $pk = 'address_id';
    protected $name = 'member_address';

    use ModelTrait;
    protected $autoWriteTimestamp = true;
    //protected $dateFormat = 'Y-m-d';
    //protected $createTime = 'create_time';
    //protected $updateTime = 'update_time';

    /**
    * {@inheritdoc}
    */
    public function attributeLabels()
    {
        return [
            'address_id' => '表id',
            'member_id' => '用户id',
            'consignee' => '收货人',
            'email' => '邮箱地址',
            'country' => '国家',
            'province' => '省份',
            'city' => '城市',
            'district' => '地区',
            'twon' => '乡镇',
            'address' => '地址',
            'zipcode' => '邮政编码',
            'mobile' => '手机',
            'is_default' => '默认收货地址',
            'is_pickup' => '',
        ];
    }

    /**
     * 地址添加/编辑
     * @param $member_id 用户id
     * @param $member_id 地址id(编辑时需传入)
     * @return array
     */
    public static function add_address($member_id,$address_id=0,$data){
        $post = $data;
        $post['is_default'] = isset($post['is_default'])?$post['is_default']:0;
        if($address_id == 0)
        {
            $c = self::where("member_id", $member_id)->count();
            if($c >= 20)
                return array('status'=>-1,'msg'=>'最多只能添加20个收货地址','result'=>'');
        }

        //检查手机格式
        if($post['consignee'] == '')
            return array('status'=>-1,'msg'=>'收货人不能为空','result'=>'');
        if(!$post['province'] || !$post['city'] || !$post['district'])
            return array('status'=>-1,'msg'=>'所在地区不能为空','result'=>'');
        if(!$post['address'])
            return array('status'=>-1,'msg'=>'地址不能为空','result'=>'');
        if(!check_mobile($post['mobile']))
            return array('status'=>-1,'msg'=>'手机号码格式有误','result'=>'');

        //编辑模式
        if($address_id > 0){

            $address = self::where(array('address_id'=>$address_id,'member_id'=> $member_id))->find();
            if($post['is_default'] == 1 && $address['is_default'] != 1)
                self::where(array('member_id'=>$member_id))->save(array('is_default'=>0));
            $row = self::where(array('address_id'=>$address_id,'member_id'=> $member_id))->save($post);
            if(!$row)
                return array('status'=>-1,'msg'=>'操作完成','result'=>'');
            return array('status'=>1,'msg'=>'编辑成功','result'=>'');
        }
        //添加模式
        $post['member_id'] = $member_id;

        // 如果目前只有一个收货地址则改为默认收货地址
        $c = self::where("member_id", $post['member_id'])->count();
        if($c == 0)  $post['is_default'] = 1;

        $address_id = self::create($post);
        //如果设为默认地址
        $insert_id = Db::name('member_address')->getLastInsID();
        $map['member_id'] = $member_id;
        $map['address_id'] = array('neq',$insert_id);

        if($post['is_default'] == 1)
            self::where($map)->save(array('is_default'=>0));
        if(!$address_id)
            return array('status'=>-1,'msg'=>'添加失败','result'=>'');


        return array('status'=>1,'msg'=>'添加成功','result'=>$address_id);
    }
}


?>